<?php
/**
*
* @package bb4dd2.0x
* @version $Id: functions_counter.php,v 1.010 2008/02/29 01:26:00 nedka Exp $
* @copyright (c) 2006, 2008 nedka (Nguyen Dang Khoa)
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* Original this script from Simple Animated Counter PHP 1.1 by Chi Kien Uong (http://www.proxy2.de)
* Re-written by nedka (Nguyen Dang Khoa) for meeting with phpBB3 Olympus :p
* I used database to store hits value of counter, so you do not need a file called "counter.txt" as old counter-mods for phpBB2.
* You can reset counter (started date time & hits value) and changes some its settings easily from ACP :)
*/

class display_counter
{
	var $config = array();

	function display_counter()
	{
		global $config, $phpbb_root_path;

		$this->config['counter_digits_path'] = $phpbb_root_path . $config['counter_digits_path'];
		$this->config['counter_digits_ani_path'] = $phpbb_root_path . $config['counter_digits_ani_path'];
		$this->config['counter_digits_number'] = $config['counter_digits_number'];
		$this->config['counter_block_ip'] = $config['counter_block_ip'];
		$this->config['counter_block_time'] = $config['counter_block_time'];
		$this->config['counter_ip_logfile'] = $phpbb_root_path . $config['counter_ip_logfile'];
	}

    function is_new_visitor()
	{
		$is_new = true;
		$rows = @file($this->config['counter_ip_logfile']);
		$this_time = time();
		$ip = getenv('REMOTE_ADDR');
		$reload_dat = fopen($this->config['counter_ip_logfile'], 'wb');

		flock($reload_dat, 2);

		for ($i = 0; $i < sizeof($rows); $i++)
		{
			list($time_stamp, $ip_addr) = split("\|", $rows[$i]);

			if ($this_time < ($time_stamp + $this->config['counter_block_time']))
			{
				if (chop($ip_addr) == $ip)
				{
					$is_new = false;
				}
				else
				{
					fwrite($reload_dat, "$time_stamp|$ip_addr");
				}
			}
		}

		fwrite($reload_dat, "$this_time|$ip\n");
		flock($reload_dat, 3);
		fclose($reload_dat);

		return $is_new;
	}

	function read_counter()
	{
		global $config, $phpbb_root_path;

		$update = false;
		$this->counter = $config['counter_hits'];

		if ($this->config['counter_block_ip'])
		{
			if ($this->is_new_visitor())
			{
				$this->counter++;
				$update = true;
			}
		}
		else
		{
			$this->counter++;
			$update = true;
		}

		if ($update)
		{
			set_config('counter_hits', $this->counter, true);
		}

		return $this->counter;
	}

	function create_output()
	{
		$this->read_counter();
		$this->counter = sprintf("%0" . "" . $this->config['counter_digits_number'] . "" . "d", $this->counter);

		$ani_digits = sprintf("%0" . "" . $this->config['counter_digits_number'] . "" . "d", $this->counter + 1);
		$html_output = '';

		for ($i = 0; $i < strlen($this->counter); $i++)
		{
			if (substr("$this->counter", $i, 1) == substr("$ani_digits", $i, 1))
			{
				$digit_pos = substr("$this->counter", $i, 1);
				$html_output .= '<img src="' . $this->config['counter_digits_path'] . '/' . $digit_pos . '.gif"';
			}
			else
			{
				$digit_pos = substr("$ani_digits", $i, 1);
				$html_output .= '<img src="' . $this->config['counter_digits_ani_path'] . '/' . $digit_pos. '.gif"';
			}

			$html_output .= ' alt="" />';
		}

		return $html_output;
	}
}

?>